package start.demo01.datatype;

import org.junit.Test;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

public class TimeTrans {
    @Test
    public void t0() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        LocalDateTime ldt = LocalDateTime.parse("2018-01-12 17:07:05", df);
        System.out.println("LocalDateTime转成String类型的时间：" + localTime);
        System.out.println("String类型的时间转成LocalDateTime：" + ldt);
        System.out.println(Timestamp.from(Instant.ofEpochMilli(System.currentTimeMillis())));
        System.out.println(Timestamp.from(Instant.ofEpochMilli(1623058562000L)));
    }
    @Test
    public void t1() throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.parse("2022-08-01 00:00:00").getTime());
        sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.parse("2022-08-30 00:00:00").getTime());
    }

    @Test
    public void t2(){
        long current = System.currentTimeMillis();
        long zero = current - (current+TimeZone.getDefault().getRawOffset())%(1000L*3600*24);
        System.out.println(zero);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(zero));
    }
}
